/**
 *
 * 城市绩效(别名：城市绩效)-明细视图
 *
 * @author HandsomeHan
 * @date 2018/12/7 13:46:38
 */
layui.define(['index', 'table', 'laydate','util', 'laytpl', 'form','zmnBind', 'layer'], function (exports) {
    var table = layui.table,
        date = layui.laydate,
        form = layui.form, util = layui.util,
        $ = layui.jquery,
        zmnBind = layui.zmnBind,
        laytpl = layui.laytpl,
        layer = layui.layer,
        param = {}

    date.render({
        elem: '#searchTime',
        type: 'month',
        value: new Date(),
        btns: ['now', 'confirm'],
        format: 'yyyyMM'
    });
    let cols = [
        [
            {field: 'manageCompanyName', title: '服务公司', align: 'center'},
            {field: 'cityName', title: '城市', align: 'center'},
            {field: 'discardRate', title: '弃单率', align: 'center', templet: e => formatterData(e.discardRate)},
            {field: 'sysDiscardRate', title: '系统弃单率', align: 'center', templet: e => formatterData(e.sysDiscardRate)},
            {field: 'visitFailRate', title: '上门失败率', align: 'center', templet: e => formatterData(e.visitFailRate)},
            {
                field: 'noVisitFailRate',
                title: '未上门失败率',
                align: 'center',
                templet: e => formatterData(e.noVisitFailRate)
            },
            {field: 'reworkRate', title: '返修率', align: 'center', templet: e => formatterData(e.reworkRate)},
            {
                field: 'reworkSuccessRate',
                title: '返修成功占比',
                align: 'center',
                templet: e => formatterData(e.reworkSuccessRate)
            },
            {field: 'partRate', title: '配件率', align: 'center', templet: e => formatterData(e.partRate)},
            // {field: 'completeRate', title: '订单完成率', align: 'center', templet: e => formatterData(e.completeRate)},
            {field: 'successRate', title: '成功率', align: 'center', templet: e => formatterData(e.successRate)},
            {field: 'userPayAmountRate', title: '用户现金支付', align: 'center', templet: e => formatterData(e.userPayAmountRate)}

        ]
    ];

    function formatterData(val) {
        if (val == null || val == 0 || val == undefined) {
            return "0%";
        }
        return toDecimal(val * 100) + "%";
    }

    //功能：将浮点数四舍五入，取小数点后2位
    function toDecimal(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return;
        }
        f = Math.round(x * 100) / 100;
        return f;
    }

    let tableOption = {
        elem: '#dataTable'
        , url: '/perf/city/ajax/list.action'
        , cols
        , cellMinWidth: 80
        , loading: true
        , page: true
        , toolbar: true
        , defaultToolbar: []
        , limit: 50
        , limits: [50, 100, 200]
        , where:{month: util.toDateString(new Date(), 'yyyyMM')}
        , response: {
            statusCode: 200
        }
        , height: 'full-135'
        , request: {
            pageName: 'pageIndex'
            , limitName: 'pageSize'
        }
        , done: function (res) {
            //添加按钮
            let layout = [{
                title: '帮助',
                layEvent: 'CUSTOM_HELP',
                icon: 'layui-icon-help'
            }, {
                title: '导出',
                layEvent: 'CUSTOM_EXPORT',
                icon: 'layui-icon-export',
                display: toolbarCompetence
            }];
            ReportUtil.toobarRightAddButton(layout)
            //渲染总计行
            ReportUtil.renderTotalRow(res.extData);
            // 修复reload的时候会携带上次查询参数
            param = this.where;
            document.tableIns = this;
        }
    };

    let tableobj = table.render(tableOption);
    //监听提交事件
    form.on('submit(zmn-form-submit)', function (res) {
        for (const fieldKey in res.field) {
            if (res.field[fieldKey] === "") {
                delete res.field[fieldKey];
            }
        }
        document.tableIns.where = {};

        tableobj.reload({
            where: res.field,
            page: {curr: 1}
        });
        return false;
    });
    form.on('submit(zmn-form-reset)', function (res) {
        location.reload();
    });
    //监听头工具时间
    table.on('toolbar(dataTable)', function (obj) {
        switch (obj.event) {
            case 'CUSTOM_EXPORT':
                let that = this;
                let ele = that.querySelector('i');
                ReportUtil.toggleLoading(ele);
                that.setAttribute('lay-event', '');
                zmnUtil.download("/perf/city/outputExcel.action", param, "城市绩效" + new Date().toISOString() + '.xls', (res) => {
                    that.setAttribute("lay-event", 'CUSTOM_EXPORT');
                    ReportUtil.toggleLoading(ele);
                });
                break;
            case "CUSTOM_HELP":
                ReportUtil.tips(help_content.innerHTML, this);
                break;
        }
    });
    // 子公司
    zmnBind.selectSubCompany("companyId", null, () => ({plat: platMark.ZMN}), true, true);
    // 服务商
    zmnBind.searchSpCompany("manageCompanyId", () => ({
        plat: platMark.ZMN,
        type: 130
    }), true, true);
    zmnBind.searchSpCompany("manageCompanyId", () => ({
        plat: platMark.ZMN,
        type: 130
    }), true, true);
    zmnBind.selectCity("cityId", '', null, true, true);

    exports('perfCity');
});
